﻿using System;
using UnityEngine;

namespace UFrame
{
    /// <summary>
    /// 使用案列
    ///        if (ft.IsState() == FloatTimeCounter.CounterState.Run){
    ///             Debug.Log("冷却 ");
    ///             return;
    ///         }
    /// </summary>
    public class FloatTimeCounter
    {
        public float time;
        private float timeCounter = 0f;
        public Action onComplete;
        public bool isActive = true;
        public bool isOne = false;
        public CounterState state = CounterState.None;
        
        
        
        /// <summary>
        /// 用于计算
        /// </summary>
        public bool isFirstNoCounter = false;

        public FloatTimeCounter()
        {
        }

        public FloatTimeCounter(float time, Action onComplete)
        {
            this.time = time;
            this.onComplete = onComplete;
            ResetCounter();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isFirstCounter">第一次不计时间</param>
        public FloatTimeCounter(float time, Action onComplete, bool isFirstNoCounter)
        {
            if (isFirstNoCounter)
            {
                this.isFirstNoCounter = isFirstNoCounter;
                timeCounter = time;
            }

            this.time = time;
            this.onComplete = onComplete;
            ResetCounter();
        }

        public float GetPassTime()
        {
            return timeCounter;
        }

        /// <summary>
        /// None 没有启动
        /// Run 正在计时
        /// Complete 计时完毕  一帧状态
        /// </summary>
        public CounterState IsState()
        {
            IsReachSus();
            return state;
        }

        /// <summary>
        /// true 代表计时完成，一帧状态
        /// false 代表正在计时 或 没有启动
        /// </summary>
        public bool IsReachSus()
        {
            if (state == CounterState.Complete && isActive == false) //需要控制下一帧改会状态
                state = CounterState.None;

            if (!isActive)
                return false;

            state = CounterState.Run; //运行状态

            timeCounter += Time.deltaTime;
            if (timeCounter >= time)
            {
                onComplete?.Invoke();
                ResetCounter();

                if (isOne)
                {
                    isActive = false;
                    state = CounterState.Complete; //完成状态
                }

                return true;
            }

            return false;
        }


        public void ResetCounter()
        {
            timeCounter = 0f;
        }

        public void Launch()
        {
            if (isActive)
                return;
            
            Debug.Log("启动成功");
            if (this.isFirstNoCounter)
            {
                this.isFirstNoCounter = false;
                timeCounter = time;
            }

            timeCounter = 0;
            isActive = true;
        }


        public enum CounterState
        {
            None,
            Run,
            Complete
        }
    }
}